package com.java.tutorials.jdk7.datagramchannel;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.NetworkInterface;
import java.net.StandardProtocolFamily;
import java.net.StandardSocketOptions;
import java.nio.channels.DatagramChannel;
import java.nio.channels.MembershipKey;
/**
 * Created by xuzhike on 2016/1/23
 * http://www.java2s.com/Code/Java/JDK-7/MembershipKeyandDatagramChannel.htm.
 * MembershipKey and DatagramChannel
 */
public class DatagramChannelTest {
    public static void main(String[] args) throws Exception {
        NetworkInterface networkInterface = NetworkInterface.getByName("net1");

        DatagramChannel dc = DatagramChannel.open(StandardProtocolFamily.INET);

        dc.setOption(StandardSocketOptions.SO_REUSEADDR, true);
        dc.bind(new InetSocketAddress(8080));
        dc.setOption(StandardSocketOptions.IP_MULTICAST_IF, networkInterface);

        InetAddress group = InetAddress.getByName("180.90.4.12");
        MembershipKey key = dc.join(group, networkInterface);
    }
}
